/**
 * @author Helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.BankAccountDao) SALE.dao.local.BankAccountDao = function(){
	var bankAccountDoc;
}

SALE.dao.local.BankAccountDao.prototype = {	
		
		initDoc: function() {
			if(!this.bankAccountDoc) {
				this.bankAccountDoc = LocalXMLFileUtil.getBankAccountDoc();
			}
			return this.bankAccountDoc;
		},
		
		saveDoc : function() {
			LocalXMLFileUtil.saveXML(this.bankAccountDoc,LocalXMLFileUtil.getBankAccountFile());
		},
		
		insertBankAccountInfo : function(accountJson) {
			var root = this.bankAccountDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			accountJson["id"] = id;
			root.setAttribute("maxid",id);
			var accountNode = this.bankAccountDoc.createElement("account");
			
			this.updateBankAccount(accountJson, accountNode);			
			root.appendChild(accountNode);
			
			this.saveDoc();
		},
		
		modifyBankAccountInfo : function(accountJson) {
			var accountNode = this.searchBankAccountByID(accountJson.id);
			
			this.updateBankAccount(accountJson, accountNode);
			
			this.saveDoc();
		},
		
		deleteBankAccountInfo : function(id) {
			var accountNode = this.searchBankAccountByID(id);
			var root = this.bankAccountDoc.documentElement;
			if(accountNode != null) {
				root.removeChild(accountNode);
			}			
		},
				
		updateBankAccount : function(accountJson, accountNode) {
			
			for(var attri in accountJson) {	
				
				if(attri.indexOf("old") != 0 && accountJson[attri] && accountJson[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, accountNode);
					if(attriNodes != null && attriNodes.length > 0) {
						accountNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.bankAccountDoc.createElement(attri);
					var contextNode = this.bankAccountDoc.createCDATASection(accountJson[attri]);
					attriNode.appendChild(contextNode);
					accountNode.appendChild(attriNode);
				}
			}
		},
		
		searchBankAccountByID : function(id) {
			var root = this.bankAccountDoc.documentElement;
			var objArray = Ext.query("/account:has(id:nodeValue(" + id+ "))", root);
			var result;
			
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchDefaultBankAccountJson : function() {
			var root = this.bankAccountDoc.documentElement;
			var objArray = Ext.query("/account:has(default:nodeValue(true))", root);
			var result = {};
			
			if (objArray.length > 0) {
				var children = objArray[0].childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		searchBankAccountJsonByID : function(id) {
			
			var resultNode = this.searchBankAccountByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildBankAccountsJson: function() {
			var root = this.initDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/account", root);
				var accountsJson = {accounts: []};
				
				Ext.each(objArray, function(account) {
					var children = account.childNodes;
					var accountJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!accountJson[child.nodeName] && child.childNodes[0] != null) {
								accountJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					accountsJson.accounts.push(accountJson);
				});
				
				return accountsJson;
			}
		}	
}
